﻿/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "SQLStorages.h"
#include <cstdarg>
#include <NodeCpp/String.h>
#include "Shared/Storage/SQLStorageImpl.h"
#include "Shared/ProgressBar.h"
#include "Shared/Log/Log.h"
#include "SQLStorageFmt.h"
#include "GlobalVars.h"
#include "DBCStores.h"

// 加载数据库发生错误
void __OnSQLStorageLoadError(const char* _Format, ...)
{
	char _Msg[256];
	std::va_list _Arg;
	va_start(_Arg, _Format);
	NodeCpp::vsnprintf(_Msg, sizeof(_Msg), _Format, _Arg);
	va_end(_Arg);

	sLog.outDbError(_Msg);
}

SQLStorages::SQLStorages(void)
    : CreatureStorage_(CreatureInfosrcfmt, CreatureInfodstfmt, "entry", "creature_template")
	, InstanceTemplate_(InstanceTemplatesrcfmt, InstanceTemplatedstfmt, "map", "instance_template")
    , ItemStorage_(ItemPrototypesrcfmt, ItemPrototypedstfmt, "entry", "item_template")
{

}

SQLStorages::~SQLStorages(void)
{

}


struct SQLCreatureLoader : public SQLStorageLoaderBase<SQLCreatureLoader, SQLStorage>
{
    /* 从字符串名字获取脚本ID */
    template<class D>
    void convert_from_str(std::uint32_t /*field_pos*/, char const* _Src, D& _Dst) {
        _Dst = D(0);
    }
};
bool SQLStorages::_LoadCreatureTemplates(void)
{
    SQLCreatureLoader _Loader;
    _Loader.Load<BarGoLink>(sWorldDB, CreatureStorage_, true, __OnSQLStorageLoadError);

    // TODO: 检查数据正确性

    sLog.outString(">> Loaded %u creature definitions", CreatureStorage_.GetRecordCount());
    sLog.outString();
    return true;
}


struct SQLInstanceLoader : public SQLStorageLoaderBase < SQLInstanceLoader, SQLStorage >
{
    /* 从字符串名字获取脚本ID */
	template<class D>
	void convert_from_str(std::uint32_t /*field_pos*/, char const* _Src, D& _Dst) {
		_Dst = D(0);
	}
};
bool SQLStorages::_LoadInstanceTemplate(void)
{
	SQLInstanceLoader _Loader;
	_Loader.Load<BarGoLink>(sWorldDB, InstanceTemplate_, true, __OnSQLStorageLoadError);

    // TODO: 检查数据正确性

	sLog.outString(">> Loaded %u Instance Template definitions", InstanceTemplate_.GetRecordCount());
	sLog.outString();
	return true;
}

struct SQLItemLoader : public SQLStorageLoaderBase<SQLItemLoader, SQLStorage>
{
    /* 从字符串名字获取脚本ID */
    template<class D>
    void convert_from_str(std::uint32_t /*field_pos*/, char const* _Src, D& _Dst) {
        _Dst = D(0);
    }
};
bool SQLStorages::_LoadItemPrototypes(void)
{
    SQLItemLoader _Loader;
    _Loader.Load<BarGoLink>(sWorldDB, ItemStorage_, true, __OnSQLStorageLoadError);

    // TODO: 检查数据正确性

    sLog.outString(">> Loaded %u item prototypes", ItemStorage_.GetRecordCount());
    sLog.outString();
    return true;
}